import {
  sign,
  querySign
} from "../../api/score"
import {
  getUserInfo
} from "../../api/user";

var app = getApp()
Page({
  data: {
    list: [],
    index: 1,
    isFinish: false,
    loadingMoreHidden: false,
    score: 0, //积分
    score_sign_continuous: 0, //连续签到次数
    arr: [],
    sysW: null,
    lastDay: null,
    firstDay: null,
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    year: null,
    statusBarHeight: '',
    titleBarHeight: ''
  },

  onShow() {
    this.getMySign()
    var that = this;
    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }

  },
  dataTime: function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var months = date.getMonth() + 1;

    //获取现今年份
    this.data.year = year;

    //获取现今月份
    this.data.month = months;

    //获取今日日期
    this.data.getDate = date.getDate();

    //最后一天是几号
    var d = new Date(year, months, 0);
    this.data.lastDay = d.getDate();

    //第一天星期几
    let firstDay = new Date(year, month, 1);
    this.data.firstDay = firstDay.getDay();
  },
  // getData(){
  //   if(this.data.isFinish){
  //     wx.showToast({
  //       title: '没有更多了~',
  //     })
  //     return;
  //   }    
  //   app.get(`/point/get_point_record?CustId=${app.globalData.uid}&page=${this.data.index}&size=20`,res=>{
  //     console.log(res);
  //     if(res&&res.code=="0"){        
  //       if(res.result.length<=0){
  //         this.data.isFinish=true;
  //         wx.showToast({
  //           title: '没有更多了~',
  //         })
  //         return;
  //       }
  //       this.setData({
  //         list:this.data.list.concat(res.result.map(item=>{
  //           item.CreateTime=item.CreateTime.substr(0,10);       
  //           return item;
  //         }))          
  //       })   
  //       this.setData({
  //         loadingMoreHidden:this.data.list.length>0
  //       })             
  //       this.data.index=this.data.index+1;
  //     }else{
  //       this.data.isFinish=true;
  //     }
  //   })
  // },
  //签到按钮
  async scoresign() {
    var that = this
    wx.showLoading({
      title: '请等待...',
    })
    let res = await sign(wx.getStorageSync('access_token'))
    console.log(res)
    wx.hideLoading()
    if (res && res.code == 10000) {
      let userInfo = await getUserInfo(wx.getStorageSync('access_token'))
      app.globalData.userInfo = userInfo.data
      this.setData({
        score: app.globalData.userInfo.point
      })
      wx.showToast({
        title: '签到成功！'
      })
      this.getMySign()
    } else {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    }
  },
  async getMySign() {
    let time = this.getToDay()
    let res = await querySign(time, wx.getStorageSync('access_token'))
    if (res && res.code == 10000) {
      if (res.data.length > 0) {
        this.setData({
          score_sign_continuous: res.data[res.data.length - 1].signDay,
          list: res.data,
          ci: this.isToDay(new Date().valueOf(), res.data[res.data.length - 1].createTime)
        })
        // console.log(new Date().valueOf(), res.data[res.data.length - 1].createTime)
        console.log(this.isToDay(new Date().valueOf(), res.data[res.data.length - 1].createTime))
      } else {
        this.setData({
          list: res.data
        })
      }
    }
    this.dataTime()
    //根据得到今月的最后一天日期遍历 得到所有日期
    if( this.data.arr.length == 0 ){
      for (var i = 1; i < this.data.lastDay + 1; i++) {
        this.data.arr.push({
          day: i,
          signDay: false
        })
      }
    }
    for (let i = 0; i < this.data.list.length; i++) {
      for (let j = 0; j < this.data.arr.length; j++) {
        if (this.data.list[i].createTime.substring(8, 10) == this.data.arr[j].day) {
          this.data.arr[j].signDay = true
        }
      }
    }
    var response = wx.getSystemInfoSync()
    this.setData({
      sysW: response.windowHeight / 15,//更具屏幕宽度变化自动设置宽度
      marLet: this.data.firstDay,
      arr: this.data.arr,
      year: this.data.year,
      getDate: this.data.getDate,
      month: this.data.month
    })

    console.log(res)
  },
  getToDay() {
    var date = new Date()
    var sign2 = ":"
    var year = date.getFullYear() // 年
    var month = date.getMonth() + 1; // 月
    var day = date.getDate(); // 日
    var hour = date.getHours(); // 时
    var minutes = date.getMinutes(); // 分
    var seconds = date.getSeconds() //秒
    var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
    var week = weekArr[date.getDay()]
    // 给一位数的数据前面加 “0”
    if (month >= 1 && month <= 9) {
      month = "0" + month
    }
    if (day >= 0 && day <= 9) {
      day = "0" + day
    }
    if (hour >= 0 && hour <= 9) {
      hour = "0" + hour
    }
    if (minutes >= 0 && minutes <= 9) {
      minutes = "0" + minutes
    }
    if (seconds >= 0 && seconds <= 9) {
      seconds = "0" + seconds
    }
    // year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds
    return year + month
  },
  isToDay(time, listTime) {
    let date = new Date(time)//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    let nowTime = Y + M + D
    return nowTime == listTime.substring(0, 10)
  },

  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  checkScoreSign: function () {
    var that = this
    // app.postForm("/point/check_signin",{CustId:app.globalData.uid},res=>{
    //   if(res){
    //     if(res.code=="1"){
    //       if(res.result){
    //         that.setData({
    //           ci: 1,
    //           score_sign_continuous:res.result.Days
    //         });
    //         return;
    //       }
    //      if(res.code=="0") {
    //         that.setData({
    //           ci: 0
    //         });
    //       }   
    //     }
    //     if(res.code=="0"){

    //     }
    //   }else{

    //   }
    // })
  },
  onLoad: function () {
    var that = this
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight,
      titleBarHeight: app.globalData.titleBarHeight
    })
    if (app.globalData.iphone == true) {
      that.setData({
        iphone: 'iphone'
      })
    }
    wx.hideShareMenu()
    that.checkScoreSign()
    //获取积分
    this.setData({
      score: app.globalData.userInfo.point
    })
    // wx.request({
    //   url: app.globalData.urls + '/user/amount',
    //   data: {
    //     token: app.globalData.token
    //   },
    //   success: function (res) {
    //     if (res.data.code == 0) {
    //       that.setData({
    //         balance: res.data.data.balance,
    //         freeze: res.data.data.freeze,
    //         score: res.data.data.score
    //       });
    //     }
    //   }
    // })
    //获取签到规则
    that.setData({
      rules: [{
        continuous: 1,
        score: 1
      },
      {
        continuous: 2,
        score: 1
      },
      {
        continuous: 3,
        score: 1
      },
      {
        continuous: 4,
        score: 1
      },
      {
        continuous: 5,
        score: 1
      },
      {
        continuous: 6,
        score: 1
      },
      {
        continuous: 7,
        score: 1
      },
      ]
    });
    // wx.request({
    //   url: app.globalData.urls + '/score/sign/rules',
    //   data: {
    //   },
    //   success: function (res) {

    //     if (res.data.code == 0) {
    //       that.setData({
    //         rules: res.data.data
    //       });
    //     }
    //   }
    // })
    //获取签到记录

    /*wx.request({
      url: app.siteInfo.url + app.siteInfo.subDomain + '/score/sign/logs',
      data: {
        token:app.globalData.token,
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            score_sign_continuous: res.data.data.result[0].continuous
          });
        }
      }
    })*/
    // this.getData();
  },
  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-detail/goods-detail?id=" + e.currentTarget.dataset.id
    })
  },
  home: function () {
    wx.switchTab({
      url: "/pages/index/index"
    })
  },
  score: function () {
    wx.navigateTo({
      url: "/pages/coupons/coupons"
    })
  },
  home: function () {
    wx.switchTab({
      url: '../index/index'
    })
  }

})